package com.tianxin.workbao.workBao.holder;

import android.content.Context;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.adapter.SelectLoaderAdapter;
import com.tianxin.workbao.workBao.bean.Loader;
import com.tianxin.workbao.workBao.bean.LoaderAndLoaderUse;
import com.tianxin.workbao.workBao.bean.LoaderUse;
import com.tianxin.workbao.workBao.dataProvide.LoaderProvide;
import com.tianxin.workbao.workBao.dataProvide.LoaderUseProvide;
import com.tianxin.workbao.workBao.listener.MyPopupWindowListener;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hacker on 2017/7/2.
 */

public class SelectLoaderPopupWindowHolder implements UpdateListViewListener {

    private Context mContext;
    private View mContentView;
    private MyPopupWindowListener myPopupWindowListener;
    private SelectLoaderAdapter mAdapter;
    private RecyclerView mRecyclerView;
    private List<LoaderAndLoaderUse> mLoaderAndLoaderUseList = new ArrayList<>();
    private List<Loader> mLoaderList = new ArrayList<>();
    private List<LoaderUse> mLoaderUseList = new ArrayList<>();
    private LoaderProvide mLoaderProvide;
    private LoaderUseProvide mLoaderUseProvide;

    public SelectLoaderPopupWindowHolder(Context context, MyPopupWindowListener listener) {
        this.mContext = context;
        this.mContentView = LayoutInflater.from(mContext).inflate(R.layout.select_loader_popup_window, null);
        this.myPopupWindowListener = listener;
        initView();
        initData();
    }

    private void initView() {
        mRecyclerView = (RecyclerView) mContentView.findViewById(R.id.loader_recyclerView);
        mAdapter = new SelectLoaderAdapter(mLoaderAndLoaderUseList, mContext, myPopupWindowListener);
        mRecyclerView.setAdapter(mAdapter);
        //设置Item增加、移除动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        //设置布局管理器
        int spanCount = 1;
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, spanCount);
        mRecyclerView.setLayoutManager(gridLayoutManager);
    }

    private void initData() {
        getLoaderProvide().getLoaderList();
        getLoaderUseProvide().getLoaderUseList();
    }


    private void makeDate() {
        mLoaderAndLoaderUseList.clear();
        for (int i = 0; i < mLoaderList.size(); i++) {
            LoaderAndLoaderUse loaderAndLoaderUse = new LoaderAndLoaderUse();
            loaderAndLoaderUse.setLoader(mLoaderList.get(i));
            for (int j = 0; j < mLoaderUseList.size(); j++) {
                if (Integer.valueOf(mLoaderList.get(i).getId().toString()) == mLoaderUseList.get(j).getLoaderId()) {
                    loaderAndLoaderUse.setLoaderUse(mLoaderUseList.get(j));
                } else {
                    loaderAndLoaderUse.setLoaderUse(null);
                }
            }
            mLoaderAndLoaderUseList.add(i, loaderAndLoaderUse);
        }
        mAdapter.notifyDataSetChanged();
    }

    public View getContentView() {
        return mContentView;
    }

    public SelectLoaderAdapter getAdapter() {
        return mAdapter;
    }

    public LoaderProvide getLoaderProvide() {
        if (mLoaderProvide == null) {
            mLoaderProvide = new LoaderProvide(mContext, this);
        }
        return mLoaderProvide;
    }

    public LoaderUseProvide getLoaderUseProvide() {
        if (mLoaderUseProvide == null) {
            mLoaderUseProvide = new LoaderUseProvide(mContext, this);
        }
        return mLoaderUseProvide;
    }

    @Override
    public void showMessage(String msg) {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void onRefreshData(Object result) {
        if (result instanceof List) {
            if (((List) result).get(0).getClass() == LoaderUse.class) {
                mLoaderUseList.clear();
                mLoaderUseList.addAll((List<LoaderUse>) result);
            } else if (((List) result).get(0).getClass() == Loader.class) {
                mLoaderList.clear();
                mLoaderList.addAll((List<Loader>) result);
            }
        }
        makeDate();
    }

    @Override
    public void onLoadMoreData(Object result) {

    }
}
